//
//  JUFNStatusTableViewCell.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/9.
//  Copyright © 2017年 陈俊峰. All rights reserved.
//

import UIKit
import SDWebImage

//算出view的最大宽度
private let maxWidth = screenWidth - 2*commomMargin
//算出如果每行有3个的情况下.显示情况
private let itemMargin : CGFloat = 2
private let itemWidth = (maxWidth - 2 * itemMargin)/3


class JUFNStatusTableViewCell: UITableViewCell {
    
    @IBOutlet weak var AvIconImage: UIImageView!
    
    @IBOutlet weak var verifiedImgV: UIImageView!
    
    
    @IBOutlet weak var lbl_screenName: UILabel!
    
    
    @IBOutlet weak var rankImgV: UIImageView!
    
    @IBOutlet weak var lbl_creatTime: UILabel!
    
    @IBOutlet weak var lbl_source: UILabel!
    
    @IBOutlet weak var lbl_text: UILabel!
    //转发的picView
    @IBOutlet weak var picViews: JUFNPictureShowView!
    //被转发的微博正文
    @IBOutlet weak var lbl_retweet: UILabel!
    
    
    @IBOutlet weak var picFlowLayOut: UICollectionViewFlowLayout!
    
    @IBOutlet weak var picViewHeight: NSLayoutConstraint!
    //底下的转发toolBar栏
    @IBOutlet weak var toolBar: UIStackView!
    //原创微博顶部约束
    @IBOutlet weak var picTopContant: NSLayoutConstraint!
    
    @IBOutlet weak var picViewWidth: NSLayoutConstraint!
    var cellModel : JUFNHomeCellModel? {
        didSet {
            
            self.AvIconImage.sd_setImage(with:cellModel?.headURL)
            self.rankImgV.image = cellModel?.rankImage
            self.lbl_text.text = cellModel?.userModel?.text
            self.lbl_screenName.text = cellModel?.userModel?.user?.screen_name
            self.verifiedImgV.image = cellModel?.memberShipImage
            self.lbl_creatTime.text = cellModel?.creatDate
            self.lbl_source.text = cellModel?.sourceText
            self.picViews.pic_urls = cellModel?.images_url
            //根据图片个数来设置picViews的尺寸
            var picCount  = 0
            
            
            if cellModel?.userModel?.retweeted_status != nil{
                self.lbl_retweet.text = cellModel?.userModel?.retweeted_status?.text
                //                self.picViews.pic_urls = cellModel?.userModel?.retweeted_status?.pic_urls
                picCount = cellModel?.userModel?.retweeted_status?.pic_urls?.count ?? 0
            }else{
                //                self.picViews.pic_urls = cellModel?.userModel?.pic_urls
                picCount = cellModel?.userModel?.pic_urls?.count ?? 0
                picTopContant.constant = (picCount == 0) ? 0 : 8
            }
            let picSize = calcultePicViewSize(picCount: picCount)
            self.picViewHeight.constant = picSize.height
            self.picViewWidth.constant = picSize.width
            
        }
    }
    
    //    func getMembershipImage(verified_type:Int) -> UIImage? {
    //
    //        if (verified_type > 0)&&(verified_type < 7)  {
    //            return UIImage(named: "common_icon_membership_level\(userModel?.user?.mbrank)")
    //        }else{
    //            return UIImage(named: "common_icon_membership_expired")
    //        }
    //
    //    }
    //
    private func calcultePicViewSize(picCount : NSInteger) -> CGSize {
        //这边设置picFlowLayOut的妙处是解决itemSize的复用,而引起的程序图片显示异常
        picFlowLayOut.itemSize = CGSize(width: itemWidth , height: itemWidth)
        
        switch picCount {
        case 0:
            return CGSize.zero
        case 1:
            //把磁盘文件读取出来
            let cacheUrl = cellModel?.images_url?.first?.picUrl
            let cacheImage = SDWebImageManager.shared().imageCache.imageFromDiskCache(forKey: cacheUrl?.absoluteString)
            self.picFlowLayOut.itemSize = (cacheImage?.size)!
            return (cacheImage?.size)!
        case 4:
            return CGSize(width: itemWidth*2.01 + itemMargin, height: itemWidth*2 + itemMargin)
        default:
            let row = (picCount - 1)/3 + 1
            let height = CGFloat(row) * itemWidth + itemMargin * (CGFloat(row) - 1)
            return CGSize(width: maxWidth, height: height)
        }
        
        
    }
    
    //    private func getImage(rankLevel:Int) -> UIImage? {
    //        ///认证类型: -1：没有认证，0：认证用户，2,3,5: 企业认证，220: 达人(草根)
    //        if rankLevel == 2 || rankLevel == 3 || rankLevel == 5 {
    //            return UIImage(named: "avatar_enterprise_vip")!
    //        }
    //        if rankLevel == 220 {
    //            return UIImage(named:"avatar_grassroot")!
    //        }
    //        if rankLevel == 0{
    //            return UIImage(named: "avatar_vip")!
    //        }
    //        return nil
    //    }
    private func setupUI() {
        picFlowLayOut.minimumInteritemSpacing = itemMargin
        picFlowLayOut.minimumLineSpacing = itemMargin
        
        
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setupUI()
    }
    
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
